/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.consultar;

import br.com.deployment.entidade.protocolo.Protocolo;
import br.com.deployment.entidade.protocolo.ServicoProtocolo;
import br.com.deployment.entidade.selo.Selo;
import br.com.deployment.entidade.selo.TipoSelo;
import br.com.deployment.servico.ProtocoloServico;
import br.com.deployment.servico.SeloServico;
import br.com.deployment.servico.TipoSeloServico;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author elyjr
 */
@ManagedBean
@SessionScoped
public class ManagerConsultarProtocoloSelo implements Serializable{

    @EJB
    ProtocoloServico protocoloServico;
    @EJB
    TipoSeloServico tipoSeloServico;
    @EJB
    SeloServico seloServico;
    
    private Protocolo protocolo;
    private Selo selo;
    private Selo seloVer;
    private List<Protocolo> lstProtocolo;
    private ServicoProtocolo servicoProtocolo;
    private List<ServicoProtocolo> lstServicoProtocolos;
    private Integer numero;
    private Integer num1, num2;

    
    @PostConstruct
    public void init(){
        protocolo = new Protocolo();
        lstProtocolo = new ArrayList<Protocolo>();
        servicoProtocolo = new ServicoProtocolo();
        selo = new Selo();
        lstServicoProtocolos = new ArrayList<ServicoProtocolo>();
    }

     public Selo getSeloVer() {
        return seloVer;
    }

    public void setSeloVer(Selo seloVer) {
        this.seloVer = seloVer;
    }

    
    public List<Protocolo> getLstProtocolo() {
        return lstProtocolo;
    }

    public void setLstProtocolo(List<Protocolo> lstProtocolo) {
        this.lstProtocolo = lstProtocolo;
    }

    public Protocolo getProtocolo() {
        return protocolo;
    }

    public void setProtocolo(Protocolo protocolo) {
        this.protocolo = protocolo;
    }

    public ServicoProtocolo getServicoProtocolo() {
        return servicoProtocolo;
    }

    public void setServicoProtocolo(ServicoProtocolo servicoProtocolo) {
        this.servicoProtocolo = servicoProtocolo;
    }

    public Selo getSelo() {
        return selo;
    }

    public void setSelo(Selo selo) {
        this.selo = selo;
    }

    public List<ServicoProtocolo> getLstServicoProtocolos() {
        return lstServicoProtocolos;
    }

    public void setLstServicoProtocolos(List<ServicoProtocolo> lstServicoProtocolos) {
        this.lstServicoProtocolos = lstServicoProtocolos;
    }

    public Integer getNumero() {
        return numero;
    }

    public void setNumero(Integer numero) {
        this.numero = numero;
    }
    
    public void clean(){
         numero = null;
         init();
    }
    
    public void pesquisar() {

//        this.selo.setNumero(numero == null ? 0 : numero);
//        lstProtocolo = protocoloServico.tipoSeloProtocolo(selo);
//        for (Protocolo p : lstProtocolo) {
//            lstServicoProtocolos = protocoloServico.servicoProtocolos(p);
//            p.setServicoProtocolos(lstServicoProtocolos);
//          
//        }
        this.selo.setNumero(numero == null ? 0 : numero);
         lstServicoProtocolos =  protocoloServico.tipoSeloProtocolo(selo);
    }

    public void handleSelectTipoSelo(SelectEvent event) {
        TipoSelo tipoSelo = (TipoSelo) event.getObject();
        selo.setTipoSelo(tipoSeloServico.find(tipoSelo.getId()));
    }

    public void redirect(String string) {
        try {
           
            FacesContext context = FacesContext.getCurrentInstance();
            selo = new Selo();
            lstProtocolo = new ArrayList<Protocolo>();
            numero = null;
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerConsultarProtocoloSelo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
    
    public void protocoloGetSelos(int id){
        seloVer = new Selo();
        seloVer = seloServico.find(id);
        System.err.println(seloVer + "" + id);
    }
    
     public void pesquisaIntervalo() {

        //this.selo.setNumero(num1);
        //this.selo.setNumero(num2);
            this.selo.setNumero(num1 == null ? 0 : num1);
            this.selo.setNumero(num2 == null ? 0 : num2);
            lstProtocolo = protocoloServico.IntervaloSeloProtocolo(selo, num1 == null ? 0 : num1,num2 == null ? 0 : num2);
            for (Protocolo p : lstProtocolo) {
                lstServicoProtocolos = protocoloServico.servicoProtocolos(p);
                p.setServicoProtocolos(lstServicoProtocolos);

            }
        
    }

    public Integer getNum1() {
        return num1;
    }

    public void setNum1(Integer num1) {
        this.num1 = num1;
    }

    public Integer getNum2() {
        return num2;
    }

    public void setNum2(Integer num2) {
        this.num2 = num2;
    }
     
     
}